<?php
	if($_SESSION['user']['level']!=1){
	die();
	}
			require "libraries/simple_html_dom.php";
			if(isset($_POST['submit'])){
				$db=new db;
				$db->connect();
				if(trim($_POST['link'])!=""){
					$link=trim($_POST['link']);
					if(mb_strpos($link,'webtruyen.com/all')!==false){
					//echo "Đúng Link rồi";
							$html = new simple_html_dom();
							$html = file_get_html($link);
							$html= $html->find('ul.list_ct',0);
							foreach($html->find('li') as $li){
								$link2=trim( $li->find('a',0)->href);
								// Gio vao check tung truyen xem last udpate la ngay nao
								$html2= new simple_html_dom();
								$html2=file_get_html($link2);
								$fv=$html2->find('ul.fiveup',0)->find('li',0);
								$date= $fv->find('span.ngay',0)->innertext;
								$chuong=$fv->find('a',0)->innertext." ".$fv->find('a',1)->innertext;
								$status= $html2->find('span.status',0)->find('a',0)->innertext;
								$array=explode(":",$date);
								$ngay=trim($array[1]);
								$ngay=substr($ngay,-4)."-".substr($ngay,3,2)."-".substr($ngay,0,2);
								 // Query xem truyen nao
								 $db->query("select time_update,id,ten,chuongmoinhat,tinhtrang from truyen where getlink_source='".$link2."'");
								 if($db->num_rows()>0){
										 $row=$db->fetchOne();
											if($row['tinhtrang']==1){
												$row['tinhtrang']='Hoàn Thành';
											}
											if($row['tinhtrang']==2){
												$row['tinhtrang']='Đang Cập Nhật';
											}
												if($row['tinhtrang']==3){
												$row['tinhtrang']='Tạm Ngưng';
											}
								
										 if($row['time_update']!="" && $row['time_update']!='0000-00-00 00:00:00'){
											
													 $ngay2=date('Y-m-d',strtotime($row['time_update']));
													
													 if($ngay > $ngay2){
														 if($status=='Hoàn Thành'){
																 $sql="update truyen set getlink_auto=1,tinhtrang=1 where id=".$row['id'];
															}else{
																 $sql="update truyen set getlink_auto=1 where id=".$row['id'];
															}
														
														$db->nonquery($sql);
														//Dua vao mang thong tin
														$r1['id']=$row['id'];
														$r1['ten']=$row['ten'];
														$r1['mong']=$ngay2;
														$r1['tinhtrang']=$row['tinhtrang'];
														$r1['webtruyen']=$ngay;
														$r1['status']=$status;
														$r1['mong_chuong']=$row['chuongmoinhat'];
														$r1['webtruyen_chuong']=$chuong;
														 $data[]=$r1;
													 }else{
													
													 }
										 }else{
											 		$r1['id']=$row['id'];
											 		$r1['ten']=$row['ten'];
													$r1['mong']=$ngay2;
													$r1['webtruyen']=$ngay;
													$r1['tinhtrang']=$row['tinhtrang'];
													$r1['status']=$status;
													$r1['mong_chuong']=$row['chuongmoinhat'];
													$r1['webtruyen_chuong']=$chuong;
													 $data[]=$r1;
												//UPDATE AUTO SCAN NGAY
												if($status=='Hoàn Thành'){
													 $sql="update truyen set getlink_auto=1,tinhtrang=1 where id=".$row['id'];
												}else{
													 $sql="update truyen set getlink_auto=1 where id=".$row['id'];
												}
												
												$db->nonquery($sql);
												
										 }
										
								 }// Db->num_rows()>0
							
							} //End of foreach
					
					}else{
					echo "False link rồi ";
					}
				}
				
				$db->close();
			}

			loadview('template/header',$data1);
			loadview('truyen/find',$data);